﻿@inject IJSRuntime jsr

<p>
	@if (string.IsNullOrEmpty(username))
	{
		<span><a href="/register">Register</a> <a href="/login">Login</a></span>
	}
	else
	{
		<span>Hello, @username <a href="/logout">(Logout)</a></span>
	}
</p>

@code {

	private string username = string.Empty;
	private System.Threading.Timer? timer;

	protected override async Task OnInitializedAsync()
	{
		await base.OnInitializedAsync();
		timer = new System.Threading.Timer(async (object? stateInfo) =>
		{
			var userdata = await jsr.InvokeAsync<string>("localStorage.getItem", "user").ConfigureAwait(false) ?? string.Empty;
			var temp = userdata.Split(';', 2)[0];
			if (temp != username)
			{
				username = temp;
				StateHasChanged(); // NOTE: MUST CALL StateHasChanged() BECAUSE THIS IS TRIGGERED BY A TIMER INSTEAD OF A USER EVENT
			}
		}, new System.Threading.AutoResetEvent(false), 333, 333);

	}
}